package com.yjdb.sanyuantang.modules.Common.service.Impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.yjdb.sanyuantang.common.entity.SysAdmin;
import com.yjdb.sanyuantang.common.mapper.SysAdminMapper;
import com.yjdb.sanyuantang.modules.Admin.mapper.AdminLimitMapper;
import com.yjdb.sanyuantang.modules.Common.DTO.common.CommonMenuDTO;
import com.yjdb.sanyuantang.modules.Common.service.CommonLoginService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * packageName com.yjdb.sanyuantang.modules.common.service.Impl
 *
 * @author 混沌酱
 * @version 1.0
 * @className CommonLoginServiceImpl
 * @date 2024/3/15
 * @description TODO
 */
@Service
public class CommonLoginServiceImpl implements CommonLoginService {


    @Resource
    private SysAdminMapper sysAdminMapper;

    @Resource
    private AdminLimitMapper adminLimitMapper;

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public String admin_password(String username, String password) {
        Map<String, Object> filter = new HashMap<>();
        filter.put("username", username);
        List<SysAdmin> list = sysAdminMapper.selectByMap(filter);
        String token = "";
        if (!list.isEmpty()) {
            SysAdmin sysAdmin = list.get(0);
            if (sysAdmin.getPassword().equals(SaSecureUtil.sha256(password))) {
                StpUtil.login(list.get(0).getId());
                token = StpUtil.getTokenValue();
            } else {
                throw new RuntimeException("用户名或密码错误");
            }
        } else {
            throw new RuntimeException("用户名或密码错误");
        }
        return token;
    }

    /**
     * 获取菜单
     *
     * @return
     */
    public List<CommonMenuDTO> get_menu() {
        List<CommonMenuDTO> list = adminLimitMapper.getMenu(StpUtil.getPermissionList());
        return list;
    }

    /**
     * 获取子集菜单/按钮
     *
     * @param id 父级id
     * @return
     */
    @Override
    public List<CommonMenuDTO> get_menu_son(Integer id) {
        List<CommonMenuDTO> list = adminLimitMapper.get_menu_son(id);
        return list;
    }
}
